package com.kgc.sbt.provider;

import com.alibaba.dubbo.config.annotation.Service;
import com.kgc.sbt.api.BuyLimitApi;
import com.kgc.sbt.domain.LimitBuy;
import com.kgc.sbt.domain.NoticeLimit;
import com.kgc.sbt.mapper.LimitBuyMapper;
import com.kgc.sbt.mapper.NoticeLimitMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.Date;

/**
 * Created on 2021/6/21.
 * <p>
 * Author: GongMingJie
 * <p>
 * Description: 抢购模块服务
 */
@Service
@Slf4j
public class BuyLimitProvider implements BuyLimitApi {

    @Autowired
    private LimitBuyMapper limitBuyMapper;

    @Autowired
    private NoticeLimitMapper noticeLimitMapper;

    @Override
    public LimitBuy createBuyLimit(LimitBuy limitBuy) {
        limitBuy.setCreateTime(new Date());
        if(limitBuyMapper.insertSelective(limitBuy) > 0){
            return limitBuy;
        }else {
            return null;
        }
    }

    @Override
    public boolean createNoticeLimit(NoticeLimit noticeLimit) {
        noticeLimit.setCreateTime(new Date());
        return noticeLimitMapper.insertSelective(noticeLimit) > 0;
    }

    @Override
    public Integer getBuyLimitTime(Integer buyLimitId) {
        LimitBuy limitBuy = limitBuyMapper.selectByPrimaryKey(buyLimitId);
        long buyLimitTime = limitBuy.getEndTime().getTime() - limitBuy.getStartTime().getTime();
        return (int)buyLimitTime / 1000;
    }

    @Override
    public boolean isRegister(Integer userId, Integer buyLimitId) {
        if (noticeLimitMapper.selectByUserIdAndBuyLimitId(userId,buyLimitId) != null){
            return true;
        }
        return false;
    }

    @Override
    public Integer getGoodsId(Integer buyLimitId) {
        return limitBuyMapper.selectByPrimaryKey(buyLimitId).getGoodsId();
    }

    @Override
    public Date getBuyLimitStartTime(Integer buyLimitId) {
        return limitBuyMapper.selectByPrimaryKey(buyLimitId).getStartTime();
    }

    @Override
    public Date getBuyLimitEndTime(Integer buyLimitId) {
        return limitBuyMapper.selectByPrimaryKey(buyLimitId).getEndTime();
    }

    @Override
    public BigDecimal getGoodsPrice(Integer buyLimitId) {
        return limitBuyMapper.selectByPrimaryKey(buyLimitId).getPrice();
    }

    @Override
    public LimitBuy getLimitBuy(Integer id) {
        return limitBuyMapper.selectByPrimaryKey(id);
    }


}
